package com.cw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cw.constant.Constants;
import com.cw.entity.AuthorityPrehospital;
import com.cw.entity.HisCwglDept;
import com.cw.mapper.AuthorityPrehospitalMapper;
import com.cw.service.IAuthorityPrehospitalService;
import com.cw.service.IHisCwglDeptService;
import com.cw.util.UUIDUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-09-03
 */
@Service
public class AuthorityPrehospitalServiceImpl extends ServiceImpl<AuthorityPrehospitalMapper, AuthorityPrehospital> implements IAuthorityPrehospitalService {

    @Autowired
    private IHisCwglDeptService hisCwglDeptService;

    /**
     * 院前准备中心科室权限-插入
     * @param authorityPrehospital 插入的信息
     * @return java.lang.boolean
     * @author ly
     * @since 2021-09-03 10:28
     */
    @Override
    public boolean insertAuthorityPrehospital(AuthorityPrehospital authorityPrehospital) {
        LambdaQueryWrapper<AuthorityPrehospital> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AuthorityPrehospital::getDeptCode, authorityPrehospital.getDeptCode());
        lambdaQueryWrapper.eq(AuthorityPrehospital::getUserID, authorityPrehospital.getUserID());
        List<AuthorityPrehospital> list = list(lambdaQueryWrapper);
        if (!CollectionUtil.isEmpty(list)) {
            return false; }

        authorityPrehospital.setId(UUIDUtil.uuid());
        authorityPrehospital.setCreatedDate(LocalDateTime.now());
        return save(authorityPrehospital);
    }

    /**
     * 通过用户主键获取他对应的权限
     * @param userId 用户主键
     * @return java.util.List<com.cw.entity.AuthorityPrehospital>
     * @author ly
     * @since 2021-09-03 10:36
     */
    @Override
    public PageInfo<AuthorityPrehospital> getAuthorityPrehospitalbyUserId(String userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        LambdaQueryWrapper<AuthorityPrehospital> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AuthorityPrehospital::getUserID, userId);
        lambdaQueryWrapper.select(AuthorityPrehospital::getDeptCode, AuthorityPrehospital::getDeptName,AuthorityPrehospital::getId);
        return new PageInfo<>(list(lambdaQueryWrapper));
    }

    /**
     * 通过用户主键获取他对应的权限
     * @param userId 用户主键
     * @return java.util.List<com.cw.entity.AuthorityPrehospital>
     * @author ly
     * @since 2021-09-03 10:36
     */
    public List<AuthorityPrehospital> getAuthorityPrehospitalbyUserId(String userId,String yard) {
        List<AuthorityPrehospital> authorityPrehospitals = new ArrayList<>();

        LambdaQueryWrapper<AuthorityPrehospital> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AuthorityPrehospital::getUserID, userId);
        lambdaQueryWrapper.select(AuthorityPrehospital::getDeptCode, AuthorityPrehospital::getDeptName,AuthorityPrehospital::getId);
        List<AuthorityPrehospital> list = list(lambdaQueryWrapper);

        if (CollectionUtil.isEmpty(list)) { return null; }

        Optional<AuthorityPrehospital> first = list.stream().filter(value -> value.getDeptCode().equals(Constants.allDepeCode)).findFirst();
        boolean present = first.isPresent();
        //没有全部的权限直接把查出来的抛出去,正常流程就到此为止。
        //下面的代码区块是处理拥有全部
        if (!present) { authorityPrehospitals.addAll(list); return authorityPrehospitals; }
        AuthorityPrehospital authorityPrehospital = new AuthorityPrehospital();
        authorityPrehospital.setDeptCode(null);
        authorityPrehospital.setDeptName("全部");

        authorityPrehospitals.add(authorityPrehospital);
        List<HisCwglDept> hisCwglDeptByUserAccess = hisCwglDeptService.getHisCwglDeptByUserAccess(yard);
        Map<String, String> checkOutMap = new HashMap<>();
        for (HisCwglDept cwglDeptByUserAccess : hisCwglDeptByUserAccess) {
            String deptCode = cwglDeptByUserAccess.getDeptCode();

            if (deptCode.equals(Constants.allDepeCode)) { continue; }
            if (checkOutMap.containsKey(deptCode)) { continue; }
            checkOutMap.put(deptCode, deptCode);
            AuthorityPrehospital newAuthorityPrehospital = new AuthorityPrehospital();
            //拷贝属性
            BeanUtil.copyProperties(cwglDeptByUserAccess,newAuthorityPrehospital);
            authorityPrehospitals.add(newAuthorityPrehospital);
        }
        return authorityPrehospitals;
    }

}
